package com.example.lib_cache.adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.MutableLiveData;
import androidx.recyclerview.widget.RecyclerView;

import com.example.lib_cache.R;
import com.example.lib_cache.dao.OnPoiNameClickListener;
import com.example.lib_cache.dao.OnUserNickNameClickListener;
import com.example.lib_cache.databinding.ItemSelectdesBinding;
import com.example.lib_cache.databinding.NavHeadImgBinding;
import com.example.lib_cache.model.Poi;
import com.example.lib_cache.model.User;
import com.example.lib_common.util.LiveDataManager;

import java.util.List;

public class UserAdapter extends RecyclerView.Adapter<UserAdapter.UserViewHoder> {


    private User user;

    private Context context;

    public UserAdapter(User user, Context context) {
        this.user = user;
        this.context = context;
    }

    public UserAdapter(Context context) {
        this.context = context;
    }


    @NonNull
    @Override
    public UserViewHoder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        NavHeadImgBinding navHeadImgBinding = DataBindingUtil.inflate(
                LayoutInflater.from(parent.getContext()),
                R.layout.nav_head_img,
                parent,
                false);
        return new UserViewHoder(navHeadImgBinding);
    }

    @Override
    public void onBindViewHolder(@NonNull UserViewHoder holder, int position) {
        holder.navHeadImgBinding.setUserhead(user);
        holder.navHeadImgBinding.executePendingBindings();
        holder.navHeadImgBinding.navHeadImgIn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(onUserNickNameClickListener != null){
                    onUserNickNameClickListener.startEdit();
                }
            }
        });

    }

    @Override
    public int getItemCount() {
        return null == user ? 0 : 1;
    }

    public void updateData(User user){
        if (null==user){
            return;
        }
        this.user = user;
        notifyDataSetChanged();
    }

    class UserViewHoder extends RecyclerView.ViewHolder{
        NavHeadImgBinding navHeadImgBinding;

        public UserViewHoder(NavHeadImgBinding navHeadImgBinding) {
            super(navHeadImgBinding.getRoot());
            this.navHeadImgBinding = navHeadImgBinding;
        }
    }

    private OnUserNickNameClickListener onUserNickNameClickListener;

    public void setOnUserNickNameClickListener(OnUserNickNameClickListener listener){
        this.onUserNickNameClickListener = listener;
    }
}
